<?PHP
require_once 'models/models_khachhang.php';
$obkh=new models_khachhang();
require_once 'models/models_hoadon.php';
$obhd=new models_hoadon();
require_once 'models/models_cthoadon.php';
$obcthd=new models_cthoadon();
require_once 'models/models_mathang.php';
$obmh=new models_mathang();
$mod=$_GET['mod'];
switch ($mod)
{
	case 'lichsugiaodich':{
		$kh=$obkh->getkh($_GET['id']);
		$data['listhd']=$obhd->gethoadon($kh['MaKH']);
		require_once 'views/main/lichsugiaodich/lichsugiaodich.php';
		break;
	}
	case 'chitiethoadon':{
		$data = $obhd->_getHoaDon($_GET['id']);
		$data['listkh'] = $obkh->listkh();
		$data['cthd']=$obcthd->getCTHoaDon($_GET['id']);
		$data['listmh']=$obmh->listMatHang();
		require 'views/main/lichsugiaodich/chitiethoadon.php';
		break;
	}
	case 'tatca':{
		$kh=$obkh->getkh($_GET['id']);
		$data['listhd']=$obhd->gethoadon($kh['MaKH']);

		require_once 'views/main/lichsugiaodich/tatca.php';
		break;
	}
	case 'editchitiethd':{
		$data['listmh']=$obmh->listMatHang();
		$cthd=$obcthd->_getCTHoaDon($_GET['id']);
		if($_POST){
			if($cthd['MaMH']==$_POST['cmb_MatHang'])
			{
				$mh=$obmh->getMatHang($_POST['cmb_MatHang']);
				if(($mh['SoLuong']+$cthd['SoLuongBan'])>=$_POST['SoLuong'])
				{
						
					$soluong=$mh['SoLuong']+$cthd['SoLuongBan']-$_POST['SoLuong'];
					$obmh->updatemh($soluong,$_POST['cmb_MatHang']);
					$obcthd->_editCTHoaDon($_POST['cmb_MatHang'],$_POST['SoLuong'], $mh['Gia'], $_GET['id']);
				}
				else
				{
					$soluong=$mh['SoLuong']+$cthd['SoLuongBan'];
					$obmh->updatemh(0, $_POST['cmb_MatHang']);
					$obcthd->_editCTHoaDon($_POST['cmb_MatHang'],$soluong, $mh['Gia'], $_GET['id']);
				}

				echo '<script>window.location.href="index.php?mod=capnhatthanhcong"</script>';
			}
			else
			{
				$mathang=$obmh->getMatHang($cthd['MaMH']);
				$sl=$mathang['SoLuong']+$cthd['SoLuongBan'];
				$obmh->updatemh($sl,$cthd['MaMH']);
				$mh=$obmh->getMatHang($_POST['cmb_MatHang']);
				if($mh['SoLuong']>=$_POST['SoLuong'])
				{
					$soluong=$mh['SoLuong']-$_POST['SoLuong'];
					$obcthd->_editCTHoaDon($_POST['cmb_MatHang'],$_POST['SoLuong'], $mh['Gia'], $_GET['id']);
					$obmh->updatemh($soluong, $_POST['cmb_MatHang']);
				}
				else
				{
					$soluong=$mh['SoLuong'];
					$obcthd->_editCTHoaDon($_POST['cmb_MatHang'],$soluong, $mh['Gia'], $_GET['id']);
					$obmh->updatemh(0, $_POST['cmb_MatHang']);
				}
					
				echo '<script>window.location.href="index.php?mod=capnhatthanhcong"</script>';
					
			}

				
		}
		require 'views/main/lichsugiaodich/editchitiethd.php';
		break;
	}
	case 'del_cthoadon':{
			$cthd=$obcthd->_getCTHoaDon($_GET['id']);
			$mh=$obmh->getMatHang($cthd['MaMH']);
			$soluong=$mh['SoLuong']+$cthd['SoLuongBan'];
			$obmh->updatemh($soluong,$cthd['MaMH']);
			$obcthd->_delCTHoaDon($_GET['id']);
			$obhd->_delHoaDon($cthd['MaHD']);
			echo '<script>window.location.href="index.php?mod=huythanhcong"</script>';
			break;
		}
	case 'capnhatthanhcong':{
		require_once 'views/main/lichsugiaodich/capnhatthanhcong.php';
		break;
	}
	case 'huythanhcong':{
		require_once 'views/main/lichsugiaodich/huythanhcong.php';
		break;
	}
	case 'chuaduyet':{
		$kh=$obkh->getkh($_GET['id']);
		$data['listhd']=$obhd->gethoadonchuaduyet($kh['MaKH']);

		require_once 'views/main/lichsugiaodich/chuaduyet.php';
		break;
	}
	case 'daduyet':{
		$kh=$obkh->getkh($_GET['id']);
		$data['listhd']=$obhd->gethoadondaduyet($kh['MaKH']);

		require_once 'views/main/lichsugiaodich/daduyet.php';
		break;
	}
}